package com.yanqu.road.server.cmd.activity.horserun;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.HorseRunProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.horserun.CrossHorseRunActivity;
import com.yanqu.road.server.manager.activity.horserun.CrossHorseRunMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_HORSE_RUN_GET_UNION_RANCH_DATA,desc = "小马快跑获取商会马场数据和经验")
public class CrossHorseRunUserGetHorseAndExpDataCmd extends CrossUnionCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        HorseRunProto.CrossHorseRunGetUnionRanchDataAndExpReq req = HorseRunProto.CrossHorseRunGetUnionRanchDataAndExpReq.parseFrom(packet.getBytes());
        CrossHorseRunActivity activity = CrossHorseRunMgr.getActivity(req.getActivityId(), groupId);
        if (activity == null) {
            HorseRunProto.HorseRunGetUnionRanchDataAndExpResp.Builder builder = HorseRunProto.HorseRunGetUnionRanchDataAndExpResp.newBuilder();
            builder.setRet(GameErrorCode.E_HORSE_RUN_ACTIVITY_NO_EXIST);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(),
                    YanQuMessageUtils.buildMessage(GameProtocol.S_HORSE_RUN_GET_UNION_RANCH_DATA_RESP,builder.build())
                    );
            return;
        }
        //如果有上传信息 就更新下吧

        if (req.hasBriefData()){
            activity.uploadUserData(req.getUserId(),req.getBriefData());
        }

        HorseRunProto.HorseRunGetUnionRanchDataAndExpResp.Builder unionRanchDataAndGetExp = activity.getUnionRanchDataAndGetExp(req.getUserId(),req.getNeedRandomHorse());

        MessageHelper.sendPacket(serverId, packet.getPlayerId(),
                YanQuMessageUtils.buildMessage(GameProtocol.S_HORSE_RUN_GET_UNION_RANCH_DATA_RESP,unionRanchDataAndGetExp.build())
        );
    }

    @Override
    public void sendData() {

    }
}
